package org.osomit.sacct.entity;

import org.junit.Test;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertEquals;
import org.osomit.sacct.builder.ApplicationAccountRecordSetBuilder;
import org.stringtree.json.JSONReader;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jian Fang (John.Jian.Fang@gmail.com)
 * 
 * Date: Jul 1, 2009
 */
public class ApplicationAccountRecordSet_UT {

    @Test
    public void testToJSON(){
        ApplicationAccount acnt1 = new ApplicationAccount();
        acnt1.setName("cust_accnt");
        acnt1.setPassword("webcontent");
        ApplicationAccount acnt2 = new ApplicationAccount();
        acnt2.setName("production_app");
        acnt2.setPassword("PwD4ProdApp");
        List<ApplicationAccount> act1 = new ArrayList<ApplicationAccount>();
        act1.add(acnt1);
        act1.add(acnt2);
        ApplicationAccountRecord record1 = new ApplicationAccountRecord();
        record1.setClientId("server");
        record1.setAccounts(act1);

        ApplicationAccount acnt3 = new ApplicationAccount();
        acnt3.setName("johnjianfang@gmail.com");
        acnt3.setPassword("P@ssword");
        List<ApplicationAccount> act2 = new ArrayList<ApplicationAccount>();
        act2.add(acnt3);
        ApplicationAccountRecord record2 = new ApplicationAccountRecord();
        record2.setClientId("jianfang");
        record2.setAccounts(act2);

        List<ApplicationAccountRecord> records = new ArrayList<ApplicationAccountRecord>();
        records.add(record1);
        records.add(record2);

        ApplicationAccountRecordSet set = new ApplicationAccountRecordSet();
        set.setRecords(records);

        String json = set.toJSON().toString();
        System.out.println("JSON: " + json);
    }

    @Test
    public void testBuildFromJSON(){
        String json = "{\"records\":[{\"accounts\":[{\"name\":\"cust_accnt\",\"password\":\"webcontent\"},{\"name\":\"production_app\",\"password\":\"PwD4ProdApp\"}],\"clientId\":\"server\"},{\"accounts\":[{\"name\":\"johnjianfang@gmail.com\",\"password\":\"P@ssword\"}],\"clientId\":\"jianfang\"}]}";
        ApplicationAccountRecordSetBuilder builder = new ApplicationAccountRecordSetBuilder();
        JSONReader jsreader = new JSONReader();
        Object in = jsreader.read(json);
        ApplicationAccountRecordSet set = builder.build(in);
        assertNotNull(set);
        List<ApplicationAccountRecord> records = set.getRecords();
        assertNotNull(records);
        assertEquals(2, records.size());
        ApplicationAccountRecord record2 = records.get(1);
        assertEquals("jianfang", record2.getClientId());
        List<ApplicationAccount> acts = record2.getAccounts();
        assertNotNull(acts);
        assertEquals(1, acts.size());
        assertEquals("johnjianfang@gmail.com", acts.get(0).getName());
        assertEquals("P@ssword", acts.get(0).getPassword());
    }
}
